<script setup lang="ts">
import { downloadContact } from '@/apis/common';
import { getEnterpriseDetailAPI } from '@/apis/enterprise';
import type { EnterpriseDetail, Rent } from '@/types/enterprise';
import { ref } from 'vue';
import { useRoute } from 'vue-router';

// 获取页面路信息
const route = useRoute();
// 企业详情,空对象可通过as断言指定类型
// 获取企业详情
const detail = ref<EnterpriseDetail>({} as EnterpriseDetail)

// 获取企业详情
const getDetail = async () => {
  if (route.params.id) {
    const res = await getEnterpriseDetailAPI(route.params.id as string)
    console.log(res.data.rent)
    detail.value = res.data
  }
}
getDetail()

const getPreviewUrl = (url: string) => {
  const extName = url.split('.').pop();
  if (extName === 'pdf') {
    console.log('PDF URL:', url);
    return url;
  } else if (extName === 'doc' || extName === 'docx') {
    const previewURL = "https://view.officeapps.live.com/op/view.aspx?src=";
    const fullUrl = previewURL + encodeURIComponent(url);
    console.log('DOC URL:', fullUrl);
    return fullUrl;
  }
}

// 文件下载
const downloadFile = async (row:Rent) => {
  // 根据附件id下载文件,返回二进制的blob数据
  const blobData = await downloadContact(row.contractId)
  // 创建一个a标签
  const alElement = document.createElement('a');
  // 设置href属性为blob数据的URL
  alElement.href = URL.createObjectURL(blobData)
  // 设置download属性为合同文件夹
  alElement.download = row.contractName
  // 点击a标签,触发下载
  alElement.click()
  // 释放内存
  URL.revokeObjectURL(alElement.href)
  alElement.remove()
}
</script>

<template>
  <div class="detail-enterprise">
    <header class="add-header">
      <el-page-header content="查看企业" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">租赁记录</div>
        <div class="table-container">
          <el-table :data="detail.rent" style="width: 100%" border>
            <el-table-column type="index" label="序号" width="50" />
            <el-table-column prop="name" label="租赁楼宇" width="180" />
            <el-table-column label="租赁起止时间" width="280">
              <template #default="{ row }">
                {{ row.startTime }} - {{ row.endTime }}
              </template>
            </el-table-column>
            <el-table-column label="租赁合同(点击预览)">
              <template #default="{ row }">
                <el-button type="text" link>
                  <a class="link" :href="getPreviewUrl(row.contractUrl)" target="_blank">
                    {{ row.contractName }}
                  </a>
                </el-button>
              </template>
            </el-table-column>
            <el-table-column prop="createTime" label="录入时间" />
            <el-table-column label="操作">
              <template #default="{ row }">
                <el-button type="text" @click="downloadFile(row)" link>合同下载</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </main>

  </div>
</template>


<style scoped lang="scss">
.detail-enterprise {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }

      .arrow {
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;
      margin-bottom: 20px;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }
    }

    .table-container {
      padding: 20px;
    }

    .preview {
      img {
        width: 100px;
      }
    }
  }
}
</style>